package com.jlnx.controller.bank;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jlnx.dao.ChainEnterpriseDaoImpl;
import com.jlnx.dao.CoreEnterpriseDaoImpl;
import com.jlnx.dao.EnterpriseDaoImpl;
import com.jlnx.dao.FinanceReqDaoImpl;
import com.jlnx.pojo.ChainEnterprise;
import com.jlnx.pojo.CoreEnterprise;
import com.jlnx.pojo.Enterprise;
import com.jlnx.pojo.FinanceReq;

/*
 * 同意准入
 */
@WebServlet("/AgreeFinanceReqController")
public class AgreeFinanceReqController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取请求信息
		response.setContentType("application/json;charset=UTF-8;text/html");
		//设置请求编码模式
    	request.setCharacterEncoding("utf-8");
    	
    	int retId = Integer.parseInt(request.getParameter("reqId"));
    	
		 FinanceReqDaoImpl dao = new FinanceReqDaoImpl();
		 ChainEnterpriseDaoImpl chaindao = new ChainEnterpriseDaoImpl();
		 CoreEnterpriseDaoImpl coredao = new CoreEnterpriseDaoImpl();
				
		 //获取session对象
		 HttpSession hs=request.getSession();
		 //获取登录账号
		 String bankentNo = ((Enterprise)hs.getAttribute("enterprise")).getEntNo();
		 System.out.println( bankentNo + ":同意id" + retId + "融资");
		 
	     //获取融资信息，查看企业剩余额度
		 FinanceReq financeReq = dao.selectByReqId(retId);
		 String entNo = financeReq.getEntNo();
		 
		 //获取融资类型，以确定比对谁的限额和剩余额度
		 String reqTool = financeReq.getReqTool();
		 //获取依托的核心企业
		 String reply = financeReq.getReply();
		 if(reqTool.equals("保理")) {//保理：占用两方额度/************************************************/
			 System.out.println("同意融资:保理");
			 
			 ChainEnterprise chainEnterprise = chaindao.selectByEntNo(entNo);
			 CoreEnterprise coreEnterprise = coredao.selectByEntNo(reply);
			 //1。获取当前融资额度
			 BigDecimal reqAmount = financeReq.getReqAmount();
			 //2.获取链条企业剩余额度和限额
			 BigDecimal chainLimit = chainEnterprise.getLimit();
			 BigDecimal chainRemainLimit = chainEnterprise.getRemainLimit();  
			 //3.获取核心企业剩余额度和限额
			 BigDecimal coreLimit = coreEnterprise.getLimit();
			 BigDecimal coreRemainLimit = coreEnterprise.getRemainLimit();
			 System.out.println("企业：" + entNo + "融资金额" + reqAmount + "链条企业融资限额" + chainLimit + "链条企业剩余额度" + chainRemainLimit + "核心企业融资限额" + coreLimit + "核心企业剩余额度" + coreRemainLimit);
			 //判断融资是否满足要求
			 if(reqAmount.compareTo(chainLimit) > 0) {
				 System.out.println("融资请求金额超过限额!");
				 //TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "融资请求金额超过限额!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
				 return;
			 }else if(reqAmount.compareTo(coreLimit) > 0) {
				 System.out.println("融资请求金额超过核心企业剩余额度!");
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "融资请求金额超过剩余额度!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
				 return;
			 }else if(reqAmount.compareTo(chainRemainLimit) > 0) {
				 System.out.println("融资请求金额超过链条企业剩余额度!");
				 //TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "融资请求金额超过限额!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
				 return;
			 }else if(reqAmount.compareTo(coreRemainLimit) > 0) {
				 System.out.println("融资请求金额超过限额!");
				 //TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "融资请求金额超过限额!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
				 return;
			 }
			 
			 //更新企业剩余额度
			 chainEnterprise.setRemainLimit(chainRemainLimit.subtract(reqAmount));
			 coreEnterprise.setRemainLimit(coreRemainLimit.subtract(reqAmount));
			 if(chaindao.updateChainEntMessage(chainEnterprise) < 0) {
				 System.out.println("请求失败！");
				//TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "请求失败!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
			 }else if(coredao.updateCoreEntMessage(coreEnterprise) < 0){
				 System.out.println("请求失败！");
				//TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "请求失败!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
			 }else {
				 if(dao.changeReqMessageById(retId, "") < 0) {//将拒绝原因置为空
					 //TODO 失败处理
					 request.setAttribute("isRequestSuccess", false);
					 request.setAttribute("message", "请求失败!");
					 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
		         }else {
				 //更新融资状态
					 if(dao.changeReqStatusById(retId, 3) < 0) {
						//TODO 失败处理
						 request.setAttribute("isRequestSuccess", false);
						 request.setAttribute("message", "请求失败!");
						 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
					 }else {
						 request.setAttribute("isRequestSuccess", true);
						 request.setAttribute("message", "请求成功!");
						 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
					 }
		         }
			 }
			 
		 }else if(reqTool.equals("反保理")) {//反保理，占用核心企业额度/************************************************/
			 System.out.println("同意融资:反保理");
			 
			 CoreEnterprise coreEnterprise = coredao.selectByEntNo(reply);
			 //1。获取当前融资额度
			 BigDecimal reqAmount = financeReq.getReqAmount();  
			 //2.获取核心企业剩余额度和限额
			 BigDecimal coreLimit = coreEnterprise.getLimit();
			 BigDecimal coreRemainLimit = coreEnterprise.getRemainLimit();
			 System.out.println("企业：" + entNo + "融资金额" + reqAmount + "核心企业融资限额" + coreLimit + "核心企业剩余额度" + coreRemainLimit);
			 //判断融资是否满足要求
			 if(reqAmount.compareTo(coreLimit) > 0) {
				 System.out.println("融资请求金额超过核心企业剩余额度!");
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "融资请求金额超过剩余额度!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
				 return;
			 }else if(reqAmount.compareTo(coreRemainLimit) > 0) {
				 System.out.println("融资请求金额超过限额!");
				 //TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "融资请求金额超过限额!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
				 return;
			 }
			 
			 //更新企业剩余额度
			 coreEnterprise.setRemainLimit(coreRemainLimit.subtract(reqAmount));
			 if(coredao.updateCoreEntMessage(coreEnterprise) < 0){
				 System.out.println("请求失败！");
				//TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "请求失败!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
			 }else {
				 if(dao.changeReqMessageById(retId, "") < 0) {//将拒绝原因置为空
					 //TODO 失败处理
					 request.setAttribute("isRequestSuccess", false);
					 request.setAttribute("message", "请求失败!");
					 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
		         }else {
				 //更新融资状态
					 if(dao.changeReqStatusById(retId, 3) < 0) {
						//TODO 失败处理
						 request.setAttribute("isRequestSuccess", false);
						 request.setAttribute("message", "请求失败!");
						 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
					 }else {
						 request.setAttribute("isRequestSuccess", true);
						 request.setAttribute("message", "请求成功!");
						 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
					 }
		         }
			 }
			 
		 }else {//其他情况，只占用链条企业额度/************************************************/
			 System.out.println("同意融资:其他");
			 
			 ChainEnterprise chainEnterprise = chaindao.selectByEntNo(entNo);
			 //获取企业剩余额度和限额
			 BigDecimal limit = chainEnterprise.getLimit();
			 BigDecimal remainLimit = chainEnterprise.getRemainLimit();
			 
			 BigDecimal reqAmount = financeReq.getReqAmount();
			 
			 System.out.println("企业：" + entNo + "融资金额" + reqAmount + "融资限额" + limit + "剩余额度" + remainLimit);
			 //判断融资是否满足要求
			 if(reqAmount.compareTo(limit) > 0) {
				 System.out.println("融资请求金额超过限额!");
				 //TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "融资请求金额超过限额!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
				 return;
			 }
			 if(reqAmount.compareTo(remainLimit) > 0) {
				 System.out.println("融资请求金额超过剩余额度!");
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "融资请求金额超过剩余额度!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
				 return;
			 }
			 
			 //更新企业剩余额度
			 chainEnterprise.setRemainLimit(remainLimit.subtract(reqAmount));
			 if(chaindao.updateChainEntMessage(chainEnterprise) < 0) {
				 System.out.println("请求失败！");
				//TODO 失败处理
				 request.setAttribute("isRequestSuccess", false);
				 request.setAttribute("message", "请求失败!");
				 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
			 }else {
				 if(dao.changeReqMessageById(retId, "") < 0) {//将拒绝原因置为空
						//TODO 失败处理
					 request.setAttribute("isRequestSuccess", false);
					 request.setAttribute("message", "请求失败!");
					 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
		         }else {
				 //更新融资状态
					 if(dao.changeReqStatusById(retId, 3) < 0) {
						//TODO 失败处理
						 request.setAttribute("isRequestSuccess", false);
						 request.setAttribute("message", "请求失败!");
						 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
					 }else {
						 request.setAttribute("isRequestSuccess", true);
						 request.setAttribute("message", "请求成功!");
						 request.getRequestDispatcher("/FinanceHandleController").forward(request, response);
					 }
		         }
			 }
			 
			 
			 
		 }
		 





		//发起还款，修改还款状态
//		if(dao.returnById(reqId)<0) {
//			request.getRequestDispatcher("/main/returnReq.jsp").forward(request, response);
//		}else {
//			request.getRequestDispatcher("/ReturnStatusSearchController").forward(request, response);
//		}
				
	}

}
